﻿//********************************************************************
// 
//  file:  frmLogin.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Win32;

namespace Diomede.Explorer
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            UserName = txtUsername.Text;
            Password = txtPassword.Text;
            Save = chkSave.Checked;
            if (cboEndpoint.SelectedIndex >= 0)
            {
                CustomEndpoint endpoint = (CustomEndpoint) cboEndpoint.SelectedItem;
                ServiceUrl = endpoint.ServiceUrl;
                TransferUrl = endpoint.TransferUrl;
            }
            this.DialogResult = DialogResult.OK;
            Close();
        }

        public string UserName { get; set; }
        public string Password { get; set; }
        public string ServiceUrl { get; set; }
        public string TransferUrl { get; set; }
        public bool Save { get; set; }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            loadEndpoints();
            loadFromRegistry();
        }

        public void SaveEndpoint()
        {
            //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //if (!config.AppSettings.Settings["customEndpoint"].Value.Split(",".ToCharArray()).Contains(ServiceUrl))
            //{
            //    config.AppSettings.Settings.Add("customEndpoint", ServiceUrl);
            //    config.Save(ConfigurationSaveMode.Full);
            //}
        }

        void loadFromRegistry()
        {
            RegistryKey rk = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Diomede Storage\\Diomede Explorer");
            if (rk.GetValue("user") != null) txtUsername.Text = StringManip.DecodeFrom64(rk.GetValue("user").ToString());
            if (rk.GetValue("pass") != null) txtPassword.Text = StringManip.DecodeFrom64(rk.GetValue("pass").ToString());
            rk.Close();
            if (!string.IsNullOrEmpty(txtPassword.Text) && !string.IsNullOrEmpty(txtUsername.Text))
                chkSave.Checked = true;
            

        }

        void loadEndpoints()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings["customEndpoint"] == null)
            {
                config.AppSettings.Settings.Add("customEndpoint", new CustomEndpoint { Name = "Production", ServiceUrl = "http://service.diomedestorage.com", TransferUrl = "http://transfer.diomedestorage.com" }.ToSaveFormat());
                config.Save(ConfigurationSaveMode.Full);
            }

            cboEndpoint.Items.Clear();
            string[] values = config.AppSettings.Settings["customEndpoint"].Value.Split(",".ToCharArray());
            foreach (string item in values)
            {
                cboEndpoint.Items.Add(new CustomEndpoint(item));
            }
            cboEndpoint.DropDownStyle = ComboBoxStyle.DropDown;
            cboEndpoint.Refresh();
            cboEndpoint.SelectedIndex = 0;

        }

        private void btnAddEndpoint_Click(object sender, EventArgs e)
        {
            frmEndpoint frm = new frmEndpoint();
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                config.AppSettings.Settings.Add("customEndpoint", new CustomEndpoint { Name = frm.DisplayName, ServiceUrl = frm.ServiceUrl, TransferUrl = frm.TransferUrl }.ToSaveFormat());
                config.Save(ConfigurationSaveMode.Full);
                loadEndpoints();
            }
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }

        private void chkAdvanced_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAdvanced.Checked)
            {
                this.Height = this.Height + 28;
                btnLogin.Location = new Point(btnLogin.Location.X, btnLogin.Location.Y + 28);
                btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 28);
                btnCreateNew.Location = new Point(btnCreateNew.Location.X, btnCreateNew.Location.Y + 28);
                label3.Visible = true;
                cboEndpoint.Visible = true;
                btnAddEndpoint.Visible = true;
            }
            else
            {
                this.Height = this.Height - 28;
                btnLogin.Location = new Point(btnLogin.Location.X, btnLogin.Location.Y - 28);
                btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y - 28);
                btnCreateNew.Location = new Point(btnCreateNew.Location.X, btnCreateNew.Location.Y - 28);
                label3.Visible = false;
                cboEndpoint.Visible = false;
                btnAddEndpoint.Visible = false;
            }
        }

        private void btnCreateNew_Click(object sender, EventArgs e)
        {
            frmCreateAccount frm = new frmCreateAccount();
            frm.Show();
        }

        
    }



    public class CustomEndpoint
    {
        public CustomEndpoint() { }
        public CustomEndpoint(string fromSaveString)
        {
            string[] v = fromSaveString.Split("|".ToCharArray());
            Name = v[0];
            ServiceUrl = v[1];
            TransferUrl = v[2];
        }
        public string Name { get; set; }
        public string ServiceUrl { get; set; }
        public string TransferUrl { get; set; }

        public string ToSaveFormat()
        {
            return string.Format("{0}|{1}|{2}", Name, ServiceUrl, TransferUrl);
        }

        public override string ToString()
        {
            return Name;
        }
    }


    //}

    //public class CustomEndpointElement : ConfigurationElement
    //{
    //    public CustomEndpointElement()
    //    { 
    //    }
    //    public CustomEndpointElement(string name, string serviceUrl, string transferUrl)
    //    {
    //        this.Name = name;
    //        this.ServiceUrl = serviceUrl;
    //        this.TransferUrl = transferUrl;
    //    }

    //    [ConfigurationProperty("name",IsRequired=true)]
    //    public string Name {
    //        get {
    //            return (string)this["name"];
    //        }
    //        set {
    //            this["name"] = value;
    //        }
    //    }

    //    [ConfigurationProperty("serviceUrl", IsRequired=true)]
    //    public string ServiceUrl {
    //        get {
    //            return (string)this["serviceUrl"];
    //        }
    //        set {
    //            this["serviceUrl"] = value;
    //        }
    //    }

    //    [ConfigurationProperty("transferUrl",IsRequired = true)]
    //    public string TransferUrl {
    //        get {
    //            return (string)this["transferUrl"];
    //        }
    //        set {
    //            this["transferUrl"] = value;
    //        }
    //    }

    //}
}
